package com.oscar.ehome;

import android.app.Application;

import android.content.Context;

import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.os.StrictMode;
import android.support.multidex.MultiDex;

import android.util.DisplayMetrics;
import android.util.Log;



import com.oscar.ehome.utils.util.ToastUtil;
import com.oscar.ehome.utils.util.VersionUtil;

import com.taobao.sophix.PatchStatus;
import com.taobao.sophix.SophixManager;
import com.taobao.sophix.listener.PatchLoadStatusListener;
import com.umeng.analytics.MobclickAgent;

import com.umeng.socialize.PlatformConfig;




/**
 * Created by mengwanhe on 2017/5/19.
 */

public class App extends Application {

    private static DemoHandler handler;
    /**
     * 应用未启动, 个推 service已经被唤醒,保存在该时间段内离线消息(此时 GetuiSdkDemoActivity.tLogView == null)
     */
    public static StringBuilder payloadData = new StringBuilder();

    public static Application CONTEXT;
    public static int mScreenWidth = 0;
    public static int mScreenHeight = 0;
    public static String clientId = "";

    @Override
    public void onCreate() {
        super.onCreate();

        initHotFix();

        //获取屏幕宽高
        CONTEXT = this;
        DisplayMetrics mDisplayMetrics = getApplicationContext().getResources()
                .getDisplayMetrics();
        App.mScreenWidth = mDisplayMetrics.widthPixels;
        App.mScreenHeight = mDisplayMetrics.heightPixels;

        //错误统计
        MobclickAgent.setDebugMode(true);
        MobclickAgent.setScenarioType(getApplicationContext(), MobclickAgent.EScenarioType.E_UM_NORMAL);

        //7.0以上拍照处理
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
            StrictMode.setVmPolicy(builder.build());
        }

        if (handler == null) {
            handler = new DemoHandler();
        }

    }

    //各个平台的配置，建议放在全局Application或者程序入口
    {
        PlatformConfig.setWeixin("wxee0a2be42d61bc7d", "ecb6ae906c4501e819d7dc27426582e5");
        //豆瓣RENREN平台目前只能在服务器端配置
        PlatformConfig.setSinaWeibo("2363710994", "38a4f8204cc784f81f9f0daaf31e02e3", "http://sns.whalecloud.com");
        PlatformConfig.setQQZone("1106003049", "SndrgEYDebYxJ2MC");
    }

    /**
     * 分割 Dex 支持
     *
     * @param base
     */
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(base);
    }

    public static void sendMessage(Message msg) {
        handler.sendMessage(msg);
    }

    //推送handler
    public static class DemoHandler extends Handler {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    payloadData.append((String) msg.obj);
                    payloadData.append("\n");
                    Log.d("mwh", msg.obj + "msg.obj");
                    break;
                case 1:
                    ToastUtil.e("mwh", msg.obj + "msg.objssssssss");
                    break;
            }
        }
    }

    /**
     * 检查补丁包
     */
    public void initHotFix() {
        String verison = VersionUtil.getVersionName(getApplicationContext());

        // initialize最好放在attachBaseContext最前面
        SophixManager.getInstance().setContext(this)
                .setAppVersion(verison)
                .setAesKey(null)
                .setEnableDebug(true)
                .setPatchLoadStatusStub(new PatchLoadStatusListener() {
                    @Override
                    public void onLoad(final int mode, final int code, final String info, final int handlePatchVersion) {
                        // 补丁加载回调通知
//                        Toast.makeText(getApplicationContext(), "code" + code, Toast.LENGTH_LONG).show();

                        switch (code) {
                            case PatchStatus.CODE_LOAD_SUCCESS://成功
//                                Toast.makeText(getApplicationContext(), "加载布丁成功", Toast.LENGTH_LONG).show();
                                break;
                            case PatchStatus.CODE_LOAD_RELAUNCH://重启
//                                Toast.makeText(getApplicationContext(), "需要重启", Toast.LENGTH_LONG).show();
                                // 表明新补丁生效需要重启. 开发者可提示用户或者强制重启;
                                // 建议: 用户可以监听进入后台事件, 然后调用killProcessSafely自杀，以此加快应用补丁，详见1.3.2.3
                                break;
                            case 6://服务端没有最新可用的补丁
                                break;
                            case 13://加载补丁失败
                                SophixManager.getInstance().cleanPatches();
                                break;
                            case 16://密钥错误
                                break;
                            case 18://一键清除补丁
                                break;
                            case 19://连续两次queryAndLoadNewPatch()方法调用不能短于3s
                                break;
                        }
                    }
                }).initialize();
        // queryAndLoadNewPatch不可放在attachBaseContext 中，否则无网络权限，建议放在后面任意时刻，如onCreate中
        SophixManager.getInstance().queryAndLoadNewPatch();
    }

}


